package com.nsn.vs.sdm.component.svn.command;

import java.io.File;

import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import com.nsn.vs.sdm.component.svn.SvnClientManager;
import com.nsn.vs.sdm.component.svn.event.UpdateEventHandler;
import com.nsn.vs.sdm.util.string.UtilString;

/**
 * Used for executing check out repository operation.
 * 
 * Available properties from operation are: revisionProperty
 * 
 * @author Jeccy.Zhao
 */
public class SvnCommandCheckout extends SvnCommand
{

	/**
	 * The repository address
	 */
	private String url;

	/**
	 * The local path
	 */
	private String path;

	/**
	 * The sole constructor
	 * 
	 * @param manager
	 *            the svn client manager
	 * @param url
	 *            the repository address
	 * @param path
	 *            the local path
	 */
	public SvnCommandCheckout(SvnClientManager manager, String url, String path)
	{
		super(manager);

		this.url = url;
		this.path = UtilString.revertUnicodeChars(path);
		
		// sets the event handler for checkout operations
		manager.getManager().getUpdateClient().setEventHandler(
				new UpdateEventHandler(this));
		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.nsn.hzta.tcr.component.svn.Command#execute()
	 */
	@SuppressWarnings("deprecation")
	@Override
	public long execute() throws Exception
	{
		SVNURL svnurl = SVNURL.parseURIDecoded(url);

		// Get the Update Client
		SVNUpdateClient updateClient = this.svnClient.getManager()
				.getUpdateClient();

		// Execute svn info
		return updateClient.doCheckout(svnurl, new File(path), pegRevision,
				revision, depth, force);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.nsn.hzta.tcr.component.svn.Command#validateAttributes()
	 */
	@Override
	protected void validateAttributes() throws Exception
	{
		if (this.url == null)
		{
			throw new Exception("url must be provided");
		}

		if (this.path == null)
		{
			throw new Exception("path must be provided");
		}
	}

	public String getUrl()
	{
		return url;
	}

	public void setUrl(String url)
	{
		this.url = url;
	}

}
